#include <stdio.h>

#define MAX 10
#define MAX_OUTPUT 1000

int stack[MAX], top = -1;
int sequence[MAX], seqCount = 0;
int elements[MAX];
int allSequences[MAX_OUTPUT][MAX], allSeqCount = 0;

// Function to push an element into the stack
void push(int value) {
    if (top < MAX - 1) {
        stack[++top] = value;
    }
}

// Function to pop and return an element from the stack
int pop() {
    if (top >= 0) {
        return stack[top--];
    }
    return -1;
}

// Recursive function to generate all possible sequences of stack operations
void generateSequences(int n, int in, int out) {
    if (out == n) {
        // If all elements have been included in the sequence, store it
        for (int i = 0; i < seqCount; i++) {
            allSequences[allSeqCount][i] = sequence[i];
        }
        allSeqCount++;
        return;
    }

    if (in < n) {
        // Handle the case of pushing an element into the stack
        push(elements[in]);
        generateSequences(n, in + 1, out);
        pop(); // Backtrack
    }

    if (top != -1) {
        // Handle the case of popping an element from the stack
        sequence[seqCount++] = pop();
        generateSequences(n, in, out + 1);
        push(sequence[--seqCount]); // Backtrack
    }
}

int main() {
    int n;
    scanf("%d", &n); // Input the number of elements

    for(int i = 0; i < n; i++) {
        scanf("%d", &elements[i]); // Reading each element
    }

    generateSequences(n, 0, 0);

    // Print all sequences in reverse order
    for (int i = allSeqCount - 1; i >= 0; i--) {
        for (int j = 0; j < n; j++) {
            printf("%d", allSequences[i][j]);
            if(j!=n-1)
            {
            	printf(" ");
			}
        }
        if(i!=0)
        printf("\n");
    }

    return 0;
}
